package cn.etg.product2503.service.impl;

import cn.etg.product2503.entity.PageBean;
import cn.etg.product2503.entity.Product;
import cn.etg.product2503.mapper.ProductMapper;
import cn.etg.product2503.service.ProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Override
    public void createProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public Product selectbyname(String name) {
        return productMapper.selectByName(name);
    }

    @Override
    public void deleteProduct(Long id) {
        productMapper.deleteById(id);
    }

    @Override
    public void updateProduct(Long id, Product product) {
        product.setId(id);
        productMapper.updateById(product);
    }

    @Override
    public PageBean<Product> queryProduct(Integer pageNum, Integer pageSize, String keyword) {
        PageBean<Product> pageBean = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Product> products = productMapper.queryProduct(keyword);
        Page<Product> page = (Page<Product>) products;
        pageBean.setTotal(page.getTotal());
        pageBean.setPages(page.getPages());
        pageBean.setItems(page.getResult());
        return pageBean;
    }

}
